package interfaceGraphique;

import facade.FacadeAnneeEtude;
import facade.FacadePersonne;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import metier.ElementListe;
import metier.ExceptionMetier;

public class GestionGroupeGUI extends JPanel implements ActionListener {
        private FacadeAnneeEtude facadeAnneeEtude = new FacadeAnneeEtude();

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JComboBox jComboBox = null;
	private JLabel jLabel1 = null;
	private JComboBox jComboBox1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField1 = null;
	private JPanel jPanel = null;
	private JScrollPane jScrollPane = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JPanel jPanel1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JButton jButton4 = null;
	private JPanel jPanelGroupe = null;
	private JList jList = null;
         private FacadePersonne facadePersonne = new FacadePersonne();
    private ElementListe elNouveauGroupe = null;
	/**
	 * This is the default constructor
	 */
	public GestionGroupeGUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {

		GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
		gridBagConstraints15.insets = new Insets(0, 0, 0, 0);
		gridBagConstraints15.gridy = 0;
		gridBagConstraints15.ipadx = 0;
		gridBagConstraints15.ipady = 0;
		gridBagConstraints15.gridx = 0;
		this.setSize(572, 530);
		this.setLayout(new GridBagLayout());
		this.add(getJPanelGroupe(), gridBagConstraints15);
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setPreferredSize(new Dimension(200,20));
			jComboBox.setMinimumSize(new Dimension(200,20));
                        jComboBox.addPopupMenuListener(new PopupMenuListener() {
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    refreshListeSemestre();
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
            });
                        jComboBox.addActionListener(this);
                        refreshListeSemestre();
		}
		return jComboBox;
	}

	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox1() {
		if (jComboBox1 == null) {
			jComboBox1 = new JComboBox();
			jComboBox1.setPreferredSize(new Dimension(200,20));
			jComboBox1.setMinimumSize(new Dimension(200,20));
                        jComboBox1.addPopupMenuListener(new PopupMenuListener() {
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    refreshListeGroupe();
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
            });
                        jComboBox1.addActionListener(this);
                        refreshListeGroupe();
		}
		return jComboBox1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(200,20));
			jTextField.setMinimumSize(new Dimension(200,20));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(75,20));
			jTextField1.setMinimumSize(new Dimension(75,20));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.insets = new Insets(5, 5, 5, 0);
			gridBagConstraints18.gridy = 1;
			gridBagConstraints18.anchor = GridBagConstraints.WEST;
			gridBagConstraints18.gridx = 1;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints17.gridy = 1;
			gridBagConstraints17.anchor = GridBagConstraints.WEST;
			gridBagConstraints17.gridx = 0;
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.fill = GridBagConstraints.BOTH;
			gridBagConstraints16.gridx = 0;
			gridBagConstraints16.gridy = 0;
			gridBagConstraints16.ipadx = 0;
			gridBagConstraints16.weightx = 1.0;
			gridBagConstraints16.weighty = 1.0;
			gridBagConstraints16.anchor = GridBagConstraints.CENTER;
			gridBagConstraints16.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints16.gridwidth = 2;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBorder(BorderFactory.createTitledBorder(null, "Liste d\'incompatibilites", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanel.add(getJScrollPane(), gridBagConstraints16);
			jPanel.add(getJButton(), gridBagConstraints17);
			jPanel.add(getJButton1(), gridBagConstraints18);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setWheelScrollingEnabled(true);
			jScrollPane.setPreferredSize(new Dimension(200,300));
			jScrollPane.setViewportView(getJList());
			jScrollPane.setMinimumSize(new Dimension(200,300));

		}
		return jScrollPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("+");
                        jButton.setEnabled(false);
                         jButton.addActionListener(this);
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("-");
                        jButton1.setEnabled(false);
                         jButton1.addActionListener(this);
		}
		return jButton1;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.insets = new Insets(0, 0, 0, 10);
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.insets = new Insets(0, 0, 0, 10);
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getJButton2(), gridBagConstraints9);
			jPanel1.add(getJButton3(), gridBagConstraints10);
			jPanel1.add(getJButton4(), new GridBagConstraints());
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setName("Ajouter");
			jButton2.setText("Ajouter");
                        jButton2.setEnabled(false);
                        jButton2.addActionListener(this);
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("Modifier");
                        jButton3.setEnabled(false);
                         jButton3.addActionListener(this);
		}
		return jButton3;
	}

	/**
	 * This method initializes jButton4	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setText("Supprimer");
                        jButton4.setEnabled(false);
                         jButton4.addActionListener(this);
		}
		return jButton4;
	}

	/**
	 * This method initializes jPanelGroupe	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelGroupe() {
		if (jPanelGroupe == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.insets = new Insets(10, 10, 10, 15);
			gridBagConstraints14.gridx = 1;
			gridBagConstraints14.gridy = 7;
			gridBagConstraints14.ipadx = 0;
			gridBagConstraints14.anchor = GridBagConstraints.WEST;
			gridBagConstraints14.gridwidth = 2;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.anchor = GridBagConstraints.CENTER;
			gridBagConstraints11.gridheight = 2;
			gridBagConstraints11.gridwidth = 2;
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.gridy = 3;
			gridBagConstraints11.ipadx = 0;
			gridBagConstraints11.ipady = 0;
			gridBagConstraints11.weightx = 0.0;
			gridBagConstraints11.weighty = 0.0;
			gridBagConstraints11.insets = new Insets(5, 10, 5, 10);
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.NONE;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.ipadx = 0;
			gridBagConstraints7.weightx = 0.0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints7.gridx = 2;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.insets = new Insets(0, 10, 0, 0);
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.NONE;
			gridBagConstraints4.gridy = 1;
			gridBagConstraints4.ipadx = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.gridx = 2;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 1;
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.NONE;
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 3;
			gridBagConstraints3.ipadx = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new Insets(0, 10, 5, 15);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.insets = new Insets(5, 10, 0, 0);
			gridBagConstraints2.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.WEST;
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.ipadx = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(5, 10, 5, 15);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = -1;
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.insets = new Insets(10, 10, 0, 0);
			gridBagConstraints.gridy = -1;
			jPanelGroupe = new JPanel();
			jPanelGroupe.setLayout(new GridBagLayout());
			jPanelGroupe.setBorder(BorderFactory.createTitledBorder(null, " Gestion de Groupe", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jLabel3 = new JLabel();
			jLabel3.setText("Nombre d'etudiants: ");
			jLabel2 = new JLabel();
			jLabel2.setText("Nom:");
			jLabel1 = new JLabel();
			jLabel1.setText("Choix Groupe : ");
			jLabel = new JLabel();
			jLabel.setText("Filtre  Semestre :");
			jPanelGroupe.add(jLabel, gridBagConstraints);
			jPanelGroupe.add(jLabel1, gridBagConstraints2);
			jPanelGroupe.add(getJComboBox(), gridBagConstraints1);
			jPanelGroupe.add(getJComboBox1(), gridBagConstraints3);
			jPanelGroupe.add(jLabel2, gridBagConstraints6);
			jPanelGroupe.add(getJTextField(), gridBagConstraints4);
			jPanelGroupe.add(jLabel3, gridBagConstraints5);
			jPanelGroupe.add(getJTextField1(), gridBagConstraints7);
			jPanelGroupe.add(getJPanel(), gridBagConstraints11);
			jPanelGroupe.add(getJPanel1(), gridBagConstraints14);
		}
		return jPanelGroupe;
	}

	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJList() {
		if (jList == null) {
			jList = new JList();
		}
		return jList;
	}

         private void refreshListeSemestre() {
        try {
            ArrayList<ElementListe> liste = facadeAnneeEtude.creerListeSemestre();
            ElementListe e = (ElementListe) getJComboBox().getSelectedItem();
            getJComboBox().removeAllItems();
            for (int i = 0; i < liste.size(); i++) {
                getJComboBox().addItem(liste.get(i));
                if (e != null) {
                    if (e.getId() == liste.get(i).getId()) {
                        getJComboBox().setSelectedIndex(i);
                    }
                }
            }
            refreshListeGroupe();
        } catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        }
    }

             private void afficherStatut(String message) {
		AdminGUI.jLabelStatus.setText(message);
	}

             private void refreshListeGroupe() {
        try {
            int id = -1;
            if(getJComboBox().getItemCount() > 0 )
                id = ((ElementListe) getJComboBox().getSelectedItem()).getId();
            ElementListe e = (ElementListe) getJComboBox1().getSelectedItem();

            ArrayList<ElementListe> liste;
            getJButton2().setEnabled(false);
            getJComboBox1().removeAllItems();
            getJComboBox1().setEnabled(false);
            if (id != -1) {
                liste = facadePersonne.creerListeGroupeSemestre(id);
                getJComboBox1().setEnabled(true);
                getJButton().setEnabled(true);


                liste.add(0, getElNouveauGroupe());

                for (int i = 0; i < liste.size(); i++) {
                    getJComboBox1().addItem(liste.get(i));
                    if (e != null) {
                    if (e.getId() == liste.get(i).getId()) {
                        getJComboBox1().setSelectedIndex(i);
                    }
                }

                }
                //refreshListeIncompatibilite();
            }


        } catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        }
    }

             private ElementListe getElNouveauGroupe() {
        if (elNouveauGroupe == null) {
            elNouveauGroupe = new ElementListe(-1, "Nouveau Groupe");
        }
        return elNouveauGroupe;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(getJComboBox())){
            refreshListeGroupe();
        }
        else if(e.getSource().equals(getJComboBox1())){
            int selectedItem = getJComboBox1().getSelectedIndex();
            if(selectedItem == 0){
                getJTextField().setText("");
                getJTextField1().setText("");
                getJButton2().setEnabled(true);
                getJButton3().setEnabled(false);
                    getJButton4().setEnabled(false);

                    //refreshListeIncompatibilite();

            }
            else if(selectedItem > 0){
                try {
                    int id = ((ElementListe) getJComboBox1().getSelectedItem()).getId();
                    facadePersonne.creerGroupe();
                    facadePersonne.getGroupe().load(id);
                    getJTextField().setText(facadePersonne.getGroupe().getNom());
                    getJTextField1().setText(String.valueOf(facadePersonne.getGroupe().getNombreEtudiants()));
                    getJButton2().setEnabled(false);
                    getJButton3().setEnabled(true);
                    getJButton4().setEnabled(true);
                } catch (ExceptionMetier ex) {
                    afficherStatut(ex.toString());
                }
            }
            else{
                getJButton2().setEnabled(false);
                getJButton3().setEnabled(false);
                    getJButton4().setEnabled(false);
            }
        }
        else if(e.getSource().equals(jButton2)){
            try {
                int id = ((ElementListe) getJComboBox().getSelectedItem()).getId();
                facadeAnneeEtude.creerSemestre();
                facadeAnneeEtude.getSemestre().load(id);
                facadePersonne.creerGroupe(getJTextField().getText(), Integer.parseInt(getJTextField1().getText()), facadeAnneeEtude.getSemestre());
                facadePersonne.ajouterGroupe();
                refreshListeGroupe();
                afficherStatut("Ajout effectue");
            }catch (ExceptionMetier ex) {
                afficherStatut(ex.toString());
            }catch (NumberFormatException ne){
            	 afficherStatut(ne.toString());
            }
        }
         else if (e.getSource().equals(getJButton3())) {
            try {
                facadePersonne.modifierGroupe(getJTextField().getText(), Integer.parseInt(getJTextField1().getText()), facadePersonne.getGroupe().getSemestre());
                refreshListeGroupe();
                afficherStatut("Modification Effectuee");
            } catch (ExceptionMetier ex) {
                afficherStatut(ex.toString());
            } catch (NumberFormatException ex) {
                afficherStatut(ex.toString());
            }

        } else if (e.getSource().equals(getJButton4())) {
            try {
                facadePersonne.supprimerGroupe();
                refreshListeGroupe();
                afficherStatut("Suppression Effectuee");
            } catch (ExceptionMetier ex) {
                afficherStatut(ex.toString());
            }
        }
    }


    private void refreshListeIncompatibilite() throws ExceptionMetier {
        int id = -1;
        if(getJComboBox1().getSelectedItem() != null) id = ((ElementListe) getJComboBox1().getSelectedItem()).getId();
        ArrayList<ElementListe> liste;
        try {
            liste = facadePersonne.creerListeIncompatibiliteGroupe(id);

            getJButton().setEnabled(false);
            getJButton1().setEnabled(false);

            getJList().removeAll();
            DefaultListModel modelo = new DefaultListModel();

            for (int i = 0; i < liste.size(); i++) {
                modelo.addElement(liste.get(i));
            }
            getJList().setModel(modelo);
        } catch (ExceptionMetier e) {
            // TODO Auto-generated catch block
            afficherStatut(e.toString());
        }
    }

}  //  @jve:decl-index=0:visual-constraint="0,0"
